<?php
/**
 * Created by PhpStorm.
 * User: tangbl
 * Date: 2019/01/25
 * Time: 17:00
 */
namespace app\wechat\common\logic;
use think\Db;
use think\Exception;

class Codewx {

    //token有效期2小时
    private static $expireTime = 7200;

    //用户信息参考
    private static $userInfo = [

    ];

    //设置用户信息[强制授权]
    public static function createInfo($data) {

        $flag = Db::name('user')->where(['mobile' => $data['mobile']])->field('id,mobile')->find();
        if(isset($flag['id'])) throw new Exception('该手机号已经绑定');
        $flag = Db::name('user')->where(['openid' => $data['openid']])->find();
        if (isset($flag['mobile'])) { //
            throw new Exception('该微信用户已经绑定');
        } else {
            if(empty($data['frmid'])){
                $refer_user = Db::name('user')->strict(false)->where(['yqcode' => $data['frmid']])->find();
            }
            $wechat_user = Db::name('user_wechat')->strict(false)->where(['openid' => $data['openid']])->find();
            $data = [
                'userid' => createid(),
                'frmid' => isset($refer_user['yqcode']) ? $refer_user['yqcode'] : '',
                'resid' => isset($refer_user['frmid']) ? $refer_user['frmid'] : '',
                'mobile' => $wechat_user['mobile'],
                'headwx' => $wechat_user['headwx'],
                'nickwx' => $wechat_user['nickwx'],
                'openid' => $wechat_user['openid'],
                'yqcode' => randomString(6),
                'ischeck' => 2,
                'isinfo' => 2,
                'acttime' => NOW_TIME,
                'actdate' => date('Ymd', NOW_TIME),
                'addtime' => NOW_TIME,
                'adddate' => date('Ymd', NOW_TIME),
            ];
            $flag = Db::name('user')->strict(false)->insertGetId($data);
            if ($flag) {
                $userWechat = [
                    'userid' => $data['userid'],
                    'frmid' => isset($refer_user['yqcode']) ? $refer_user['yqcode'] : '',
                    'resid' => isset($refer_user['frmid']) ? $refer_user['frmid'] : '',
                ];

                $flag1 = Db::name('user_wechat')->strict(false)->where(['openid' => $data['openid']])->update($userWechat);
                if(!$flag1) {
                    throw new Exception('更新用户信息失败');
                }
                return $data;
            }else{
                throw new Exception('更新用户信息失败');
            }
        }
    }


    public static function insertUserWechat($data,$userID){

        $user_wechat = Db::name('user')->where(['openID' =>$data['openid']])->find();
        if(!isset($user_wechat['userID'])){  //未插入微信信息,绑定微信
            Db::name('user')->where(['userID' => $userID])->update(['openID' => $data['openid'], 'unionID' => $data['unionid']?$data['unionid']:'']);
            $user_wechat['openID'] = $data['openid'];
            $user_wechat['unionID'] = $data['unionid'];
        }else{ //已插入微信信息
            if($user_wechat['userID'] != $userID){  //自动解绑
                Db::name('user')->where(['userID' => $userID])->update(['openID' => '', 'unionID' => '']);
                Db::name('user')->where(['userID' => $user_wechat['userID']])->update(['openID' => $data['openid'], 'unionID' => $data['unionid']?$data['unionid']:'']);
            }
        }
        return $user_wechat;

    }

    //签发证书(jwt_encode函数只能在linux平台使用，windows平台不能用)
    public static function issue() {

        $privateKey = config('token.pKey');
        $keyTypeEncode = config('token.keyTypeEncode');
        self::$userInfo['exp'] = NOW_TIME + self::$expireTime;
        return jwt_encode(self::$userInfo, $privateKey, $keyTypeEncode);
    }

    //签发证书(jwt_encode函数只能在linux平台使用，windows平台不能用)
    public static function issueTest($userInfo) {

        $privateKey = config('token.pKey');
        $keyTypeEncode = config('token.keyTypeEncode');
        $userInfo['exp'] = NOW_TIME + 86400;
        return jwt_encode($userInfo, $privateKey, $keyTypeEncode);
    }
}
